﻿namespace Hidistro.UI.Web.pay
{
    using Hidistro.Entities.Orders;
    using Hidistro.SaleSystem.Member;
    using Hidistro.SaleSystem.Shopping;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;

    public class app_alipay_notify_url : Page
    {
        protected OrderInfo Orders;
        protected JSShop.Model.OrderInfo Order;
        private SortedDictionary<string, string> GetRequestPost()
        {
            int index = 0;
            SortedDictionary<string, string> dictionary = new SortedDictionary<string, string>();
            string[] allKeys = base.Request.Form.AllKeys;
            for (index = 0; index < allKeys.Length; index++)
            {
                dictionary.Add(allKeys[index], base.Request.Form[allKeys[index]]);
            }
            return dictionary;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Order = new JSShop.Model.OrderInfo();
            SortedDictionary<string, string> requestPost = this.GetRequestPost();
            if (requestPost.Count > 0)
            {
                Notify notify = new Notify();
                if (notify.Verify(requestPost, base.Request.Form["notify_id"], base.Request.Form["sign"]))
                {
                    string orderId = base.Request.Form["out_trade_no"];
                    string str2 = base.Request.Form["trade_no"];
                    string str3 = base.Request.Form["trade_status"];
                    if ((str3 != "TRADE_FINISHED") && (str3 == "TRADE_SUCCESS"))
                    {
                        //OrderInfo orderInfo = ShoppingProcessor.GetOrderInfo(orderId);
                        this.Order = new Juse.Interface.OrderInterface().GetOrderInfo(orderId);
                        if (Order == null)
                        {
                            base.Response.Write("success");
                            return;
                        }
                        Order.GatewayOrderId = str2;
                        this.UserPayOrder(Order);
                    }
                    base.Response.Write("success");
                }
                else
                {
                    base.Response.Write("fail");
                }
            }
            else
            {
                base.Response.Write("无通知参数");
            }
        }

        private void UserPayOrder(JSShop.Model.OrderInfo order)
        {
            if (order.OrderStatus == JSShop.Enum.OrderStatus.BuyerAlreadyPaid)
            {
                base.Response.Write("success");
            }
            else if (Orders.CheckAction(OrderActions.BUYER_PAY) && TradeHelper.UserPayOrder(order, false))
            {
                Orders.OnPayment();
                base.Response.Write("success");
            }
        }
    }
}

